<?php
/*
 * LQueryBU examples
 * 
 * 
 * @package		LQueryBU_Examples
 * @author		Xavier Perez <xmadmax@gmail.com>
 * @license 	GNU Lesser General Public License
 * @link		http://code.google.com/p/lquerybu/
 * @version		Version 1.0.5
 *
 * 
*/

$LQBU_pathinfo = pathinfo(__FILE__);

include $LQBU_pathinfo['dirname'].DIRECTORY_SEPARATOR."..".DIRECTORY_SEPARATOR."LQueryBU".DIRECTORY_SEPARATOR."LQueryBU.php";

// Include navigation vars, as page(pag), order(ord) and orientation(sen)
include "./NavVars.php";


class index
{
    private $numRowsPerPage = 10;
    private $exampleNum = 1;
    
    function run($exp = 1)
    {
        $this->getData($exp);
    }
    
    function getData($exp,$opt="")
    {
        // Get basic url params
        list($pag,$ord,$sen) = NavVars::getNavVars();
        
        // Get DB connection
        $LQueryBU = new LQueryBU();
        
        // Set basic properties
		$LQueryBU->setProperties(array("debugQuery"=>TRUE,"fieldsInfo" => TRUE));
        
		// Check example number
		switch ($exp)
        {
            case 2; // Example 2
		        $RESULT = $LQueryBU->getDataQuery("/units/getCities",array("LIMIT_FROM"=>0+(($pag-1)*$this->numRowsPerPage),"LIMIT_TO"=>$this->numRowsPerPage,"ORDER_BY" => $ord,"ASCDESC"=>$sen));
            break;
            case 3: // Example 3
		        $RESULT = $LQueryBU->getDataQuery("/units/getCountryLanguages",array("LIMIT_FROM"=>0+(($pag-1)*$this->numRowsPerPage),"LIMIT_TO"=>$this->numRowsPerPage,"ORDER_BY" => $ord,"ASCDESC"=>$sen));
                break;
            case 4:
                $retCode = 0;
                // Get ID from URL
                $ID = NavVars::getNumeric(isset($_GET["ID"])?$_GET["ID"]:"",1,4079,1);
                // Get option from URL
                $option = isset($_GET["opt"])?$_GET["opt"]:"";
                
                // If option UPDATE
                if ($option == "w" && $ID>0)
                {
                    // Retrieved POST vars that begins with '__' (see template)
                    // return as an object
                    $RECORD = NavVars::getPostVars($_POST,"__");
                    // Basic checking of ID
                    if (isset($RECORD->ID) && $RECORD->ID == $ID)
                        $retCode = $LQueryBU->getDataQuery("/units/saveCity",$RECORD);
                }
                // Get cities
                $RESULT = $LQueryBU->getDataQuery("/units/getCity",array("CITY_CODE" => $ID),true,true);
            break;
            case 1: // Example 1
            default:
                // Get countries
		        $RESULT = $LQueryBU->getDataQuery("/units/getCountries",array("LIMIT_FROM"=>0+(($pag-1)*$this->numRowsPerPage),"LIMIT_TO"=>$this->numRowsPerPage,"ORDER_BY" => $ord,"ASCDESC"=>$sen));
		    break;
        }
        
        // Get column selected information, needed for build column names
        $RESULTINFO = $LQueryBU->sqlFieldsInfo;
        
        // Show SQL commands and debugging, uncomment to see it.
	    // var_dump($LQueryBU->dumpDebugQuery());

        // Return total rows, needed for advanced pagination 
		// var_dump($LQueryBU->numRows);
		
        // Assign dumpquery information, to see later in the template 
		$debugSQL = $LQueryBU->dumpQuery();
		
		// Include base template
      	include "./example$exp.phtml";
    }
}

// Get and check examplenum 
$exampleNum = NavVars::getNumeric(isset($_GET["exp"])?$_GET["exp"]:1,1,4,1);

$index = new Index();
$index->run($exampleNum);